import requests
import json
import time
import csv

timeStamp = time.time()
localTime = time.localtime(timeStamp)
fromDate = time.strftime("%Y-%m-%d", localTime)
localTime = time.localtime(timeStamp + 24*3600)
toDate = time.strftime("%Y-%m-%d", localTime)


class QuNarSpider:
    def __init__(self):
        self.post_url = 'https://hotel.qunar.com/napi/list'
        self.request_payload_data = json.dumps({
            "b": {
                "bizVersion": "17",
                "cityUrl": "beijing_city",
                "fromDate": fromDate,
                "toDate": toDate,
                "q": "",
                "qFrom": 3,
                "start": 20,
                "num": 20,
                "minPrice": 0,
                "maxPrice": -1,
                "level": "",
                "sort": 0,
                "cityType": 1,
                "fromForLog": 1,
                "uuid": "",
                "userName": "",
                "userId": "",
                "fromAction": "",
                "searchType": 0,
                "locationAreaFilter": [],
                "comprehensiveFilter": []
            },
            "qrt": "h_hlist",
            "source": "website"
        })
        self.headers = {
          'accept': 'application/json, text/plain, */*',
          'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
          'content-type': 'application/json;charset=UTF-8',
          'referer': 'https://hotel.qunar.com/cn/beijing_city/?fromDate=2020-07-23&toDate=2020-07-24&cityName=%E5%8C%97%E4%BA%AC',
        }

    def get_data(self):
        resp = requests.post(self.post_url, headers=self.headers, data=self.request_payload_data)
        source_resp_dict = json.loads(resp.text)
        for i in source_resp_dict['data']['hotels']:
            print(i)
        return source_resp_dict['data']['hotels']

    def run(self):
        self.get_data()


if __name__ == '__main__':
    qunar = QuNarSpider()
    qunar.run()
